﻿using DataAccessLayer;
using System;
using System.Collections.Generic;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BussinessLogicLayer
{
    public class DoiBongBLL
    {
        static QuanLyGiaiDauDataContext QLGD = new QuanLyGiaiDauDataContext();
        public static List<BangTySo> LayBangDau(string tenBang)
        {
            var doibong = from db in QLGD.DoiBongAs
                          where db.TrangThai == "Thi đấu" && SqlMethods.Like(db.MaDoiBong, "%"+tenBang+"%")
                          select new BangTySo
                          {
                              TDB = db.TenDoiBong,
                              STTD = Convert.ToInt32(db.SoTranThiDau),
                              STThang = Convert.ToInt32(db.SoTranThang),
                              STH = Convert.ToInt32(db.SoTranHoa),
                              STThua = Convert.ToInt32(db.SoTranThua),
                              TSP = Convert.ToInt32(db.TiSoPhu),
                              Diem = db.Diem
                          };
            return doibong.ToList();
        }
        public static List<DoiBongA> LayDBChodgv(int MaGD)
        {
            var doibong = from db in QLGD.DoiBongAs
                          where db.TrangThai == "Thi đấu" && db.MaGiaiDau == MaGD
                          select db;
            return doibong.ToList();
        }
        public static List<ListDoiBong> LayDoiBong()
        {
            var doibong = from db in QLGD.DoiBongAs
                          where db.TrangThai == "Thi đấu"
                          select new ListDoiBong()
                          {
                              MaDB = db.MaDoiBong,
                              TDB = db.TenDoiBong
                          };
            return doibong.ToList();
        }
        public static bool ThemDoiBong(DoiBongA entity)
        {
            try
            {
                DoiBongA DBA = new DoiBongA();
                DBA.MaDoiBong = entity.MaDoiBong;
                DBA.TenDoiBong = entity.TenDoiBong;
                DBA.MaKhoa = entity.MaKhoa;
                DBA.SoTranThiDau = entity.SoTranThiDau;
                DBA.SoTranThang = entity.SoTranThang;
                DBA.SoTranHoa = entity.SoTranHoa;
                DBA.SoTranThua = entity.SoTranThua;
                DBA.TiSoPhu = entity.TiSoPhu;
                DBA.Diem = entity.Diem;
                DBA.TrangThai = entity.TrangThai;
                DBA.MaGiaiDau = entity.MaGiaiDau;
                DoiBongB DBB = new DoiBongB();
                DBB.MaDoiBong = entity.MaDoiBong;
                DBB.TenDoiBong = entity.TenDoiBong;
                DBB.MaKhoa = entity.MaKhoa;
                DBB.SoTranThiDau = entity.SoTranThiDau;
                DBB.SoTranThang = entity.SoTranThang;
                DBB.SoTranHoa = entity.SoTranHoa;
                DBB.SoTranThua = entity.SoTranThua;
                DBB.TiSoPhu = entity.TiSoPhu;
                DBB.Diem = entity.Diem;
                DBB.TrangThai = entity.TrangThai;
                DBB.MaGiaiDau = entity.MaGiaiDau;
                QLGD.DoiBongAs.InsertOnSubmit(DBA);
                QLGD.DoiBongBs.InsertOnSubmit(DBB);
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
        public static bool CapNhatDoiBong(DoiBongA entity)
        {
            try
            {
                DoiBongA CapNhatDBA = (from db in QLGD.DoiBongAs
                                     select db).Single(dbong => dbong.MaDoiBong == entity.MaDoiBong);
                DoiBongB CapNhatDBB = (from db in QLGD.DoiBongBs
                                      select db).Single(dbong => dbong.MaDoiBong == entity.MaDoiBong);
                CapNhatDBA.MaDoiBong = entity.MaDoiBong;
                CapNhatDBA.TenDoiBong = entity.TenDoiBong;
                CapNhatDBA.MaKhoa = entity.MaKhoa;
                CapNhatDBA.SoTranThiDau = entity.SoTranThiDau;
                CapNhatDBA.SoTranThang = entity.SoTranThang;
                CapNhatDBA.SoTranHoa = entity.SoTranHoa;
                CapNhatDBA.SoTranThua = entity.SoTranThua;
                CapNhatDBA.TiSoPhu = entity.TiSoPhu;
                CapNhatDBA.Diem = entity.Diem;
                CapNhatDBA.TrangThai = entity.TrangThai;
                CapNhatDBA.MaGiaiDau = entity.MaGiaiDau;
                // Doi bong 2
                CapNhatDBB.MaDoiBong = entity.MaDoiBong;
                CapNhatDBB.TenDoiBong = entity.TenDoiBong;
                CapNhatDBB.MaKhoa = entity.MaKhoa;
                CapNhatDBB.SoTranThiDau = entity.SoTranThiDau;
                CapNhatDBB.SoTranThang = entity.SoTranThang;
                CapNhatDBB.SoTranHoa = entity.SoTranHoa;
                CapNhatDBB.SoTranThua = entity.SoTranThua;
                CapNhatDBB.TiSoPhu = entity.TiSoPhu;
                CapNhatDBB.Diem = entity.Diem;
                CapNhatDBB.TrangThai = entity.TrangThai;
                CapNhatDBB.MaGiaiDau = entity.MaGiaiDau;
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
        public static bool XoaDoiBong(string MaDB)
        {
            try
            {
                DoiBongA XoaDBA = (from dba in QLGD.DoiBongAs
                                 select dba).Single(db => db.MaDoiBong == MaDB);
                DoiBongB XoaDBB = (from dbb in QLGD.DoiBongBs
                                   select dbb).Single(db => db.MaDoiBong == MaDB);
                XoaDBA.TrangThai = "Đã xóa";
                XoaDBB.TrangThai = "Đã xóa";
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
    }
}
